﻿using System;
using System.IO;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using System.Windows;

namespace DetectApp;

/// <summary>
/// SystemSetView.xaml 的交互逻辑
/// </summary>
public partial class SystemSetView : Window
{
    public string filePath = "systemdata.json";
    private systemData _systemData;

    public SystemSetView()
    {
        InitializeComponent();
       var systemSetViewModel = new SystemSetViewModel();
        DataContext =  systemSetViewModel;
        if (File.Exists(filePath))
        {
            using (var filestream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                _systemData = JsonSerializer.Deserialize<systemData>(filestream) ?? new();
            }
        }
        else
        {
            _systemData = new systemData();
        }

       
    }

    private void ChangPass_Click(object sender, RoutedEventArgs e)
    {

        PassWordData systemDatanow = new PassWordData();

        string filePath = "Products\\systemdata.json";
        if (File.Exists(filePath))
        {
            using (var filestream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                systemDatanow = JsonSerializer.Deserialize<PassWordData>(filestream) ?? new();
            }
        }
        else
        {
            systemDatanow = new PassWordData();
        }


        if (GlobalData.Instance.LoginPeople == "操作员")
        {


            if (PbOld.Password == systemDatanow.PassWord && PbNew1.Password == PbNew2.Password)
            {

                systemDatanow.PassWord = PbNew1.Password;

                if (File.Exists(filePath)) { File.Delete(filePath); }
                using (var filestream = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write))
                {
                    JsonSerializer.Serialize(filestream, systemDatanow, new JsonSerializerOptions
                    {
                        WriteIndented = true,
                        Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
                    });
                }
                MessageBox.Show("修改成功");
            }
        }
        else if (GlobalData.Instance.LoginPeople == "工程师")
        {
            if (PbOld.Password == systemDatanow.EngPassWord && PbNew1.Password == PbNew2.Password)
            {

                systemDatanow.EngPassWord = PbNew1.Password;

                if (File.Exists(filePath)) { File.Delete(filePath); }
                using (var filestream = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write))
                {
                    JsonSerializer.Serialize(filestream, systemDatanow, new JsonSerializerOptions
                    {
                        WriteIndented = true,
                        Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
                    });
                }
                MessageBox.Show("修改成功");
            }
        }






        else
        {
            MessageBox.Show("密码错误");
        }
    }

    private void Window_Closed(object sender, EventArgs e)
    {
        //if (File.Exists(filePath)) { File.Delete(filePath); }
        //using (var filestream = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write))
        //{
        //    JsonSerializer.Serialize(filestream, _systemData, new JsonSerializerOptions
        //    {
        //        WriteIndented = true,
        //        Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
        //    });
        //}
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {

    }

    //private void SelectPath_Click(object sender, RoutedEventArgs e)
    //{
    //    Folder
    //}
}

public class systemData
{

    public string ImagePath { get; set; } = "d:/images";
    public string SaveImagePath { get; set; } = "d:/images";
    public string MeasurePath { get; set; } = "d:/手动量测";

    public string ImageReSavePath { get; set; } = "d:/ImagesRecheck";
    
}

public class PassWordData
{
    public string PassWord { get; set; } = "111111";
    public string EngPassWord { get; set; } = "999999";


}